;$(function () {
	getDeptList();
    var serverIdArr =[];
    var id = $("#id").val();
    if(id==null||id==""){
    	upload(serverIdArr);
    }else{
    	getInfo(id, serverIdArr);
    }
    submitForm();
})
var tale = new $.tale();//弹框
function upload(serverIdArr) {
    $("#dropzone").dropzone({
        paramName: "file",
        url: ctxPath+"/file/upload",
        addRemoveLinks: true,
        dictRemoveLinks: "x",
//        uploadMultiple: true,
        maxFilesize: 10,
        init: function () {
            this.on("success", function (file, msg) {
                file.serverId = msg.message;
                serverIdArr.push(msg.message);
                $("#image_files").val(serverIdArr);
            });
            this.on('error', function (files, response) {
                //文件上传失败后的操作
                console.log(files, response);
            });
            //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
            this.on("removedfile", function (file) {
            	 serverIdArr.splice($.inArray(file.serverId, serverIdArr), 1);
                 $("#image_files").val(serverIdArr);
            });
        }
    });
}


function getInfo(id, serverIdArr) {
    $.ajax({
        url: ctxPath+"/agency/workDynamic/findById/" + id,
        type: 'GET',
        success: function (d) {
            var success = d.success;
            if (success == true) {
                var data = d.data;
                umbinding.bind(data);
                ue.ready(function () {//监听编辑器实例化完成的事件
                    if (data.contentEO != null) {
                        ue.setContent(data.contentEO.content);
                    }
                });
                serverIdArr.push(data.fileIds);
                $("#image_files").val(serverIdArr);
                var myDropzone = new Dropzone("#dropzone", {
                    paramName: "file",
                    url: ctxPath+"/file/upload",
                    addRemoveLinks: true,
                    dictRemoveLinks: "x",
//        	        uploadMultiple: true,
                    maxFiles: 3,
                    maxFilesize: 10,
                    init: function () {
                        this.on("success", function (file, msg) {
                        	 file.serverId = msg.message;
                             serverIdArr.push(msg.message);
                             $("#image_files").val(serverIdArr);
                        });
                        this.on('error', function (files, response) {
                        });
                        //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
                        this.on("removedfile", function (file) {
                        	 serverIdArr.splice($.inArray(file.serverId, serverIdArr), 1);
                             $("#image_files").val(serverIdArr);
                        });
                    }
                });
                if (data.files != null) {
                    for (var i = 0; i < data.files.length; i++) {
                        var size = data.files[i].size * 1024;
                        var mockFile = {
                            name: data.files[i].srcname,
                            accepted: true,
                            size: size, //图片尺寸
                            height: "70%",
                            width: "70%",
                            type: '.jpg,.png,.gif'//图片文件类型
                        };
                        myDropzone.emit("addedfile", mockFile);
                        myDropzone.emit("thumbnail", mockFile, ctxPath+"/file/showImage/" + data.files[i].id);
                        myDropzone.emit("complete", mockFile);
                    }
                }
            }
        },
        error: function () {
            var myDropzone = new Dropzone("#dropzone", {
                paramName: "file",
                url: ctxPath+"/file/upload",
                addRemoveLinks: true,
                dictRemoveLinks: "x",
//        	        uploadMultiple: true,
                maxFiles: 3,
                maxFilesize: 10,
                init: function () {
                    this.on("success", function (file, msg) {
                        file.serverId = msg.message;
                        serverIdArr.push(msg.message);
                        $("#image_files").val(serverIdArr);
                    });
                    this.on('error', function (files, response) {
                    });
                    //删除图片的事件，当上传的图片为空时，使上传按钮不可用状态
                    this.on("removedfile", function (file) {
                        serverIdArr.splice($.inArray(file.serverId, serverIdArr), 1);
                        $("#image_files").val(serverIdArr);
                    });
                }
            });
        }

    });
}


function submitForm() {
    $("#global-form").validator({
        rules: {
        },
        fields: {
            deptId: {
                rule: "required;",
                msg: {
                    required: "必填!"
                }
            },
        title: {
                rule: "required;length(1~200)",
                msg: {
                    required: "必填!"
            },
        }
        },
        invalid: function (form, errors) {
            console.log(errors);
            $(".btn-submit").removeAttr("style");
        },
        //验证成功
        valid: function (form) {
            saveInfo();
        },
    });
}

function saveInfo() {
    var json = $("#global-form").serializeFormJSON();
    console.log(json);
    var id = $("#id").val();
    var url = ctxPath+"/agency/workDynamic/saveInfo";
    if (id != '' && id != null) {
        url = ctxPath+"/agency/workDynamic/updateInfo";
    }
    $.ajax({
        url: url,
        type: 'POST',
        data: json,
        success: function (d) {
            var success = d.success;
            if (success) {
                tale.alertOk({
                    text: '保存成功',
                    then: function () {
                        setTimeout(function () {
                            window.location.href = ctxPath+'/agency/workDynamic/list';
                        }, 500);
                    }
                });
            } else {
                tale.alertError(result.message || '保存失败');
            }
        }
    });
}

/**
 * 获取组织机构
 * @returns
 */
function getDeptList(){
	$.ajax({
        url: ctxPath+"/agency/dept/getAllList",
        type: 'GET',
        success: function(d){
        	var success = d.success;
        	 if(success==true){
        	if(d.data!=null){
        		var result ='<option value="">请选择</option>';
        		
        	$.each(d.data,function(i,item){
        		result+='<option value="'+item.id+'">'+item.name+'</option>';	
        	})
        	$("#dept_select").html(result);
        	}	 
        	}
        	 
	}
        })
}
